#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h> 
#include <stdlib.h>       
#include <string.h>
#include <pthread.h>
//#include <sys/sem.h>
#include <errno.h>
#include <sys/ipc.h>
//#include <sys/msg.h>
#include <sys/timeb.h>
#include <sys/socket.h>
//#include <sys/socketvar.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <semaphore.h>
#include <string.h>
#include <termios.h>
#include <linux/input.h>
#include <semaphore.h>
#include <linux/shm.h>
#include <asm/unistd.h>
#include <sys/poll.h>
#include <dirent.h>

#include "common.h"


void irDeal(struct recvData *recvData)
{
	printf("irDeal:package_size = %d, data:\n", recvData->count);
	int i;
	for(i = 0; i < recvData->count; i++)
	{
		printf("%x ", recvData->buf[i]);
		if(i % 10 == 9) printf("\n");
	}
	printf("\n");
	
	
	static const int powerOffkeyCode = 0x0000001a;
	static const int powerOffscanCode = 0x00000074;
	
	unsigned char *b = recvData->buf;
	if(recvData->count != 9)
	{
		char logBuf[256];
		snprintf(logBuf, 256, "recvData->count != 9 recvData->count == %d\n", recvData->count);
		LOGD(logBuf);	
		return;
	}
	
	if(b[0] > 1)
	{
		char logBuf[256];
		snprintf(logBuf, 256, "sys error ! b[0] > 1 ! b[0] = %u\n", b[0]);
		LOGD(logBuf);		
	}
	
	bool down = (bool)(b[0]);
	unsigned int keyCode, scanCode;
	changeByteToUInt(keyCode, &b[1]);
	changeByteToUInt(scanCode, &b[5]);
	
	if(keyCode == powerOffkeyCode && scanCode == powerOffscanCode && down == false)
	{
		char logBuf[256];
		snprintf(logBuf, 256, "prepare to power off because of recv ir power on/off data ! down=%u, keyCode=%d, scanCode=%d\n", b[0],keyCode,scanCode);
		LOGD(logBuf);
		
		cmdOutSysToggle();
	}
}